/**
 * gislin 2017年7月11日
 */
package com.btcode.db.result;

import java.io.BufferedReader;
import java.io.Reader;
import java.sql.Clob;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.btcode.common.StringUtil;
import org.springframework.util.StringUtils;

/**
 * @author gislin 2017年7月11日
 */
public class Row extends HashMap<String, Object> {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public String getSafeString(String key) {

        Object value = get(key);
        if (value == null) {
            return "";
        }
        return value.toString();

    }

    /**
     * 获取当前列
     */
    public String[] getColumns() {
        List<String> keys = new ArrayList<String>();
        keys.addAll(this.keySet());
        return keys.toArray(new String[keys.size()]);
    }

    /**
     * 设置字段值
     */
    public void setValue(String fieldName, Object obj) {
        if (StringUtils.isEmpty(fieldName)) {
            return;
        }
        String key = fieldName.toUpperCase().trim();
        this.put(key, obj);
    }

    /**
     * 获取字段的值，并转成双精度数字，如果为空则返回0
     */
    public double getSafeDouble(String fieldName) {
        if (StringUtils.isEmpty(fieldName)) {
            return 0;
        }

        String key = fieldName.toUpperCase().trim();
        Object value = this.get(key);
        double result = 0;
        if (value == null) {
            return 0;
        }

        try {
            result = Double.parseDouble(value + "");
        }
        catch (Exception e) {
            return 0;
        }
        return result;
    }

    /**
     * 获取日期值 gislin 2016年1月18日
     */
    public Date getDate(String fieldName) {
        if (StringUtils.isEmpty(fieldName)) {
            return null;
        }

        String key = fieldName.toUpperCase().trim();
        Object value = get(key);

        if (value == null) {
            return null;
        }

        try {
            Date date = (Date) value;
            return date;
        }
        catch (Exception e) {
            return null;
        }
    }
    /**
     * 获取字段的值，并转成数字，如果为空则返回0
     */
    public int getSafeInt(String fieldName) {
        if (StringUtil.isEmpty(fieldName)){
            return 0;
        }

        String key = fieldName.toUpperCase().trim();
        Object value = this.get(key);
        int result = 0;
        if(value == null){
            return 0;
        }

        try {
            result = Integer.parseInt(value+"");
        }
        catch (Exception e) {
            return 0;
        }
        return result;
    }

    /***
     *获取Clob类型数据
     */
    public String getSafeClobString(String key) {
        Object value = get(key);
        if (value == null) {
            return "";
        }
        String clobStr=clobToString((Clob)value);
        return clobStr;
    }

    /**
     * 将clob类型转化为String类型
     *
     */
    private String clobToString(Clob clob) {
        String clobToString = "";
        if (null == clob) {
            return clobToString;
        }
        try {
            Reader is = clob.getCharacterStream();
            BufferedReader br = new BufferedReader(is);
            String s = br.readLine();
            StringBuffer sb = new StringBuffer();
            while (s != null) {
                sb.append(s);
                s = br.readLine();
            }
            clobToString = sb.toString();
            br.close();
            is.close();
        }
        catch (Exception e) {
            e.getStackTrace();
        }
        return clobToString;
    }
}